package jwcb.javapjframework.ms_template.service.service;


import ch.qos.logback.core.testUtil.RandomUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import com.github.benmanes.caffeine.cache.Cache;
import jwcb.javapjframework.common.cache.utils.CacheUtil;
import jwcb.javapjframework.common.domain.domain.R;
import jwcb.javapjframework.common.redis.RedisService.service.RedisService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Random;
import java.util.concurrent.TimeUnit;

@Service
public class CacheTestService {

    @Autowired
    private RedisService redisService;


    @Autowired
    private Cache<String,Object> cache;
    public Integer clothGet(Long proId) {
        String key = "cloth:" + proId;
        Integer price = CacheUtil.getL2Cache(redisService, key, new TypeReference<Integer>() {}, cache);
        if(price != null) {
            return price;
        }
        Integer priceFromDb = getPriceFromDB(proId);
        if(priceFromDb != null) {
            CacheUtil.setL2Cache(redisService,key, priceFromDb,cache, 30L, TimeUnit.SECONDS);
        }
        return priceFromDb;
    }

    private Integer getPriceFromDB(Long proId) {
        return new Random().nextInt(100);
    }
}
